﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities;
using BLL;
using System.Transactions;

namespace QLTB_CTK34
{
    public partial class FormThemThietBi : Form
    {
		public FormThemThietBi()
        {
            InitializeComponent();
            this.MaximizeBox = false;
        }
        void LoadDsPhanCung()
        {
            ListViewItem item_ram = new ListViewItem("RAM");
            item_ram.Checked = true;
            item_ram.SubItems.Add("");
            listViewDsPhanCung.Items.Add(item_ram);

            ListViewItem item_chip = new ListViewItem("CHIP");
            item_chip.Checked = true;
            item_chip.SubItems.Add("");
            listViewDsPhanCung.Items.Add(item_chip);

            ListViewItem item_manhinh = new ListViewItem("MÀN HÌNH");
            item_manhinh.Checked = true;
            item_manhinh.SubItems.Add("");
            listViewDsPhanCung.Items.Add(item_manhinh);

            ListViewItem item_chuot = new ListViewItem("CHUỘT");
            item_chuot.Checked = true;
            item_chuot.SubItems.Add("");
            listViewDsPhanCung.Items.Add(item_chuot);

            ListViewItem item_banphim = new ListViewItem("BÀN PHÍM");
            item_banphim.Checked = true;
            item_banphim.SubItems.Add("");
            listViewDsPhanCung.Items.Add(item_banphim);

            ListViewItem item_ocung = new ListViewItem("Ổ CỨNG");
            item_ocung.Checked = true;
            item_ocung.SubItems.Add("");
            listViewDsPhanCung.Items.Add(item_ocung);

        }
        private void FrmThemThietBi_Load(object sender, EventArgs e)
        {
			cbbLoaiThietBi.SelectedIndex = 0;
            LoadDsPhanCung();
			cbbTinhTrang.Text=cbbTinhTrang.Items[0].ToString();
        }
        MayTinh FindMayTinhTheoMaTB()
        {
            return QLMayTinh.FindByMaTB(mtxtMaTB.Text);
        }
        public ThietBi tb;
        private void btnThem_Click(object sender, EventArgs e)
        {
            ActionResult kq = new ActionResult();
            Generic.Reconnect();
            ThuVien.TRY_CATCH(() =>
            {
                tb = new ThietBi();
                tb.MaTB = mtxtMaTB.Text;
                tb.LoaiTB = cbbLoaiThietBi.Text;
                tb.NgayLapRap = dtpNgayLapRap.Value;
                tb.NgayNhap = dtpNgayNhap.Value;
                tb.NhanHieu = txtNhanHieu.Text;
                tb.TinhTrang = cbbTinhTrang.Text;
                tb.XuatXu = txtXuatXu.Text;


                if (tb.LoaiTB.ToLower().Trim() != "máy tính")//thêm thiết bị khác(máy chiếu,máy in...)
                {
                    kq = QLThietBi.InsertTB(tb);
                    kq.Show();
                }
                else//thêm máy tính
                {
                    MayTinh mt = new MayTinh();
                    mt.MaTB = tb.MaTB;
                    mt.MaMT = txtMaMT.Text;

                    List<PhanCung> dsphancung = new List<PhanCung>();
                    for (int i = 0; i < listViewDsPhanCung.Items.Count; i++)
                    {
                        PhanCung pc = new PhanCung();
                        pc.MaMT = mt.MaMT;
                        pc.TenPhanCung = listViewDsPhanCung.Items[i].SubItems[0].Text;
                        pc.ThongTinPhanCung = listViewDsPhanCung.Items[i].SubItems[1].Text;
                        dsphancung.Add(pc);
                    }
                    kq = QLMayTinh.InsertMT(tb, mt, dsphancung);
                    kq.Show();
                }
                if (kq.Success)
                {
					QLNhatKy.ThemNhatKy("Thêm thiết bị");
                    DialogResult = DialogResult.OK;
                    Close();
                }
            });
          
        }

		private void btnHuy_Click(object sender, EventArgs e)
		{
			Close();
		}

		private void bntMacDinh_Click(object sender, EventArgs e)
		{
			mtxtMaTB.ResetText();
			mtxtMaTB.ResetText();
			txtNhanHieu.ResetText();
			txtXuatXu.ResetText();
			dtpNgayNhap.ResetText();
			dtpNgayLapRap.ResetText();
			cbbTinhTrang.ResetText();
			cbbLoaiThietBi.ResetText();
		}

		private void cbbLoaiThietBi_SelectedIndexChanged(object sender, EventArgs e)
		{
			gbdsphancung.Visible = cbbLoaiThietBi.Text == "Máy tính";
            switch (cbbLoaiThietBi.Text.Trim().ToLower())
            {
                case "máy tính":
                    mtxtMaTB.Mask = @"MT.&&&&&&&";
                    break;
                case "máy chiếu":
                    mtxtMaTB.Mask = @"M\C.&&&&&&&";
                    break;
                case "camera":
                    mtxtMaTB.Mask = @"\CMR.&&&&&&&";
                    break;
                case "bàn ghế":
                    mtxtMaTB.Mask = @"BG.&&&&&&&";
                    break;
                default:
                    break;
            }
            this.Size = gbdsphancung.Visible ? new Size(646, 408) : new Size(330, 408);
		}
        void CapNhatPC()
        {
            ListViewItem lvi = listViewDsPhanCung.SelectedItems[0];
            FormPhanCung frmPC = new FormPhanCung(lvi.SubItems[0].Text, lvi.SubItems[1].Text);
            frmPC.ShowDialog();
            lvi.SubItems[0].Text = frmPC.pc.TenPhanCung;
            lvi.SubItems[1].Text = frmPC.pc.ThongTinPhanCung;
        }
        private void listViewDsPhanCung_DoubleClick(object sender, EventArgs e)
        {
            CapNhatPC();
        }

        private void thêmPhầnCứngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormPhanCung frmphancung = new FormPhanCung();
            frmphancung.ShowDialog();
            ListViewItem lvi = new ListViewItem(frmphancung.pc.TenPhanCung);
            lvi.SubItems.Add(frmphancung.pc.ThongTinPhanCung);
            listViewDsPhanCung.Items.Add(lvi);
        }

        private void capnhatPhầnCứngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CapNhatPC();
        }

        private void xóaPhầnCứngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var count = listViewDsPhanCung.SelectedItems.Count;
            if (count > 0)
            {
                listViewDsPhanCung.Items.Remove(listViewDsPhanCung.SelectedItems[0]);
            }
        }

        private void listViewDsPhanCung_MouseEnter(object sender, EventArgs e)
        {
            if (listViewDsPhanCung.SelectedItems.Count > 0)
            {
                capnhatPhầnCứngToolStripMenuItem.Visible = true;
                xóaPhầnCứngToolStripMenuItem.Visible = true;
            }
        }
    }
}
